package com.yypinger.gxonline.view.basedata.usermanager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;
import com.yypinger.gxonline.GxOnlineApplication;
import com.yypinger.gxonline.entities.User;
import com.yypinger.gxonline.usermanager.IUserManager;
import com.yypinger.gxonline.usermanager.UserChanageListener;

public class UserFormWindow extends Window {

	private static final String COMMON_FIELD_WIDTH = "12em";
	public static final Object iso3166_PROPERTY_NAME = "name";
    public static final Object iso3166_PROPERTY_SHORT = "short";
    public static final Object iso3166_PROPERTY_FLAG = "flag";
    public static final Object hw_PROPERTY_NAME = "name";
    public static final Object hw_PROPERTY_ICON = "icon";
	
	private List<UserChanageListener> listeners = new ArrayList<UserChanageListener>();
	private IUserManager userManager;
	
	User person;
	
	public UserFormWindow(String title){
		super(title);
		setIcon(new ThemeResource("../"+GxOnlineApplication.GXONLINE_THEME_NAME+"/png/32x32/user.png"));
		setStyleName("userForm");
		
		person = new User(); // a person POJO
        BeanItem<User> personItem = new BeanItem<User>(person); // item from
        final Form personForm = new FormWithComplexLayout(personItem);
        addComponent(personForm);

        // The cancel / apply buttons
        HorizontalLayout buttons = new HorizontalLayout();
        buttons.setSpacing(true);

        Button applyBtn = new Button("保存", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                try {
                    personForm.commit();
                    System.out.println(1);
                } catch (Exception e) {
                	
                }
            }
        });
        buttons.addComponent(applyBtn);

        Button closeBtn = new Button("取消", new Button.ClickListener() {            
        	public void buttonClick(ClickEvent event) {                
        		(UserFormWindow.this.getParent()).removeWindow(UserFormWindow.this);            
        		}        
        	});
        
        buttons.addComponent(closeBtn);
        personForm.getFooter().setMargin(true);
        personForm.getFooter().addComponent(buttons);
	}
	
	
	public UserFormWindow(String title,IUserManager userManager){
		super(title);
		this.userManager = userManager;
	}
	
	public void setUserChanageListener(UserChanageListener listener){
		if(!listeners.contains(listener)){
			listeners.add(listener);
		}
	}
	
	
	private void showPojoState() {
        Window.Notification n = new Window.Notification("POJO state",
                Window.Notification.TYPE_TRAY_NOTIFICATION);
        n.setPosition(Window.Notification.POSITION_CENTERED);
        n.setDescription("First name: " + person.getName());
        getWindow().showNotification(n);
    }

	
	public class FormWithComplexLayout extends Form {

        private GridLayout ourLayout;

        public FormWithComplexLayout(BeanItem<User> personItem) {
            setCaption("会员信息");
            // Create our layout (3x3 GridLayout)
            ourLayout = new GridLayout(3, 3);
            ourLayout.setMargin(true, false, false, true);
            ourLayout.setSpacing(true);

            setLayout(ourLayout);
            setWriteThrough(false);
            setInvalidCommitted(false);
            setFormFieldFactory(new PersonFieldFactory());
            setItemDataSource(personItem); 
            //setVisibleItemProperties(Arrays.asList(new String[] { "name"}));

        }
        
        @Override
        protected void attachField(Object propertyId, Field field) {
            if (propertyId.equals("userId")) {
                ourLayout.addComponent(field, 0, 0);
            } else if (propertyId.equals("name")) {
                ourLayout.addComponent(field, 1, 0, 2, 0);
            } else if (propertyId.equals("password")) {
                ourLayout.addComponent(field, 0, 2);
            } else if (propertyId.equals("countryCode")) {
                ourLayout.addComponent(field, 0, 1, 2, 1);
            } else if (propertyId.equals("shoesize")) {
                ourLayout.addComponent(field, 1, 2);
            } else if (propertyId.equals("birthdate")) {
                ourLayout.addComponent(field, 2, 2);
            }
        }
        

    }
	
	private class PersonFieldFactory extends DefaultFieldFactory {

        public PersonFieldFactory() {
          
        }

        @Override
        public Field createField(Item item, Object propertyId,
                Component uiContext) {
            
        	Field f = super.createField(item, propertyId, uiContext);

            if ("name".equals(propertyId)) {
                TextField tf = (TextField) f;
                f.setCaption("会员名");
                tf.setRequired(true);
                tf.setRequiredError("请填写用户名！");
                tf.setWidth(COMMON_FIELD_WIDTH);
                tf.addValidator(new StringLengthValidator(
                        "First Name must be 3-25 characters", 3, 25, false));
            }else if("userId".equals(propertyId)){
            	TextField tf = (TextField) f;
                f.setCaption("会员编号");
                f.setValue("12345678");
                f.setReadOnly(true);
                tf.setWidth(COMMON_FIELD_WIDTH);
                tf.addValidator(new StringLengthValidator(
                        "First Name must be 3-25 characters", 3, 25, false));
            }
            return f;
        }

        private PasswordField createPasswordField(Object propertyId) {
            PasswordField pf = new PasswordField();
            pf.setCaption(DefaultFieldFactory
                    .createCaptionByPropertyId(propertyId));
            return pf;
        }
    }
}
